package com.kreedx.clockview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.Calendar;
import java.util.Locale;

public class ClockView extends View {
    protected int CLOCK_BACKGROUND_COLOR = 0xFFF0F0F0;
    protected int HOUR_COLOR = 0xFF5B5B5B;
    protected int MINUTE_COLOR = 0xFF5B5B5B;
    protected int SECOND_COLOR = 0xFFB55050;
    protected int DOT_COLOR = 0xFFB55050;

    private static final float REVENT_PERCENT = 0.2f;

    private int radius = 100;
    private float hourLength = 50;
    private float minuteLength = 70;
    private float secondLength = 90;

    private int rightPadding = 0;

    private int width;

    private int touchX, touchY;

    private Paint paint = new Paint();

    public ClockView(Context context) {
        this(context, null);
    }

    public ClockView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ClockView, defStyleAttr, 0);
        int indexCount = a.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = a.getIndex(i);
            if(attr == R.styleable.ClockView_clockRadius){
              radius = (int) (a.getDimension(attr , radius) + 0.5f);
            } else if (attr == R.styleable.ClockView_rightPadding) {
                rightPadding = a.getDimensionPixelSize(attr, 0);
            } else if (attr == R.styleable.ClockView_backgroundColor) {
                CLOCK_BACKGROUND_COLOR = a.getColor(attr, CLOCK_BACKGROUND_COLOR);
            } else if (attr == R.styleable.ClockView_hourColor) {
                HOUR_COLOR = a.getColor(attr, HOUR_COLOR);
            } else if (attr == R.styleable.ClockView_hourLength) {
                hourLength = a.getDimension(attr, hourLength);
            } else if (attr == R.styleable.ClockView_minuteColor) {
                MINUTE_COLOR = a.getColor(attr, MINUTE_COLOR);
            } else if (attr == R.styleable.ClockView_minuteLength) {
                minuteLength = a.getDimension(attr, minuteLength);
            } else if (attr == R.styleable.ClockView_secondColor) {
                SECOND_COLOR = a.getColor(attr, SECOND_COLOR);
            } else if (attr == R.styleable.ClockView_secondLength) {
                secondLength = a.getDimension(attr, secondLength);
            }

        }

        width = DimensionUtil.getWidth(context);
        touchX = width - radius - rightPadding;
        touchY = radius;


        LogUtil.i(String.format(Locale.ENGLISH, "rightPadding%d", rightPadding));

        a.recycle();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(widthMeasureSpec, measureHeight(getSuggestedMinimumHeight(), heightMeasureSpec));

    }

    private int measureHeight(int defaultHeight, int measureSpec) {

        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        Log.e("YViewHeight", "---speSize = " + specSize + "");

        switch (specMode) {
            case MeasureSpec.AT_MOST:
                defaultHeight = radius * 2;
                Log.e("YViewHeight", "---speMode = AT_MOST" + measureSpec);
                break;
            case MeasureSpec.EXACTLY:
                defaultHeight = specSize;
                Log.e("YViewHeight", "---speSize = EXACTLY");
                break;
            case MeasureSpec.UNSPECIFIED:
                defaultHeight = Math.max(defaultHeight, specSize);
                Log.e("YViewHeight", "---speSize = UNSPECIFIED");
//        1.基准点是baseline
//        2.ascent：是baseline之上至字符最高处的距离
//        3.descent：是baseline之下至字符最低处的距离
//        4.leading：是上一行字符的descent到下一行的ascent之间的距离,也就是相邻行间的空白距离
//        5.top：是指的是最高字符到baseline的值,即ascent的最大值
//        6.bottom：是指最低字符到baseline的值,即descent的最大值

                break;
        }
        return defaultHeight;


    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);

        canvas.save();
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setColor(CLOCK_BACKGROUND_COLOR);
        canvas.drawCircle(touchX, touchY, radius, paint);


        paint.setStrokeCap(Paint.Cap.ROUND);

        canvas.restore();
        canvas.save();
        canvas.rotate(hour % 12 * 30 - 90, touchX, touchY);
        paint.setColor(HOUR_COLOR);
        paint.setStrokeWidth(10);
        canvas.drawLine(touchX - hourLength * REVENT_PERCENT, touchY, touchX + hourLength, touchY, paint);
        canvas.restore();

        canvas.save();
        canvas.rotate(minute % 60 * 6 - 90, touchX, touchY);
        paint.setColor(MINUTE_COLOR);
        paint.setStrokeWidth(8);
        canvas.drawLine(touchX - minuteLength * REVENT_PERCENT, touchY, touchX + minuteLength, touchY, paint);
        canvas.restore();

        canvas.save();
        canvas.rotate(second % 60 * 6 - 90, touchX, touchY);
        paint.setColor(SECOND_COLOR);
        paint.setStrokeWidth(4);
        canvas.drawLine(touchX - secondLength * REVENT_PERCENT, touchY, touchX + secondLength, touchY, paint);
        canvas.restore();

        paint.setColor(DOT_COLOR);
        paint.setStrokeWidth(20);
        canvas.drawPoint(touchX, touchY, paint);

        postInvalidateDelayed(1000);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                LogUtil.i("ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                LogUtil.i("ACTION_MOVE");

                touchX = Math.max((int) event.getX(), radius);
                touchX = Math.min(DimensionUtil.getWidth(getContext()) - radius, touchX);
                //touchY = (int) event.getY();
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                LogUtil.i("ACTION_UP");
                break;
        }

        return true;
    }
}
